package com.lwx.json;

import java.lang.reflect.Type;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
 * Json转换的工具类
 * @author passover
 *
 */
public class JsonUtil {
	
	private static Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
	
	public static String toJson(Object object){
		return gson.toJson(object);
	}

	public static <T> T fromJson(String json, Class<T> classOfT){
		T obj = null;
        try{
            obj = (T) gson.fromJson(json, classOfT);
        } catch(Exception e) {
            e.printStackTrace();
            System.err.println("error json: " + json);
        }
        return obj;
	}
	
	@SuppressWarnings("unchecked")
    public static <T> T fromJson(String json, Type typeOfT) {
	    T obj = null;
	    try{
	        obj = (T) gson.fromJson(json, typeOfT);
	    } catch(Exception e) {
	        e.printStackTrace();
	        System.err.println("error json: " + json);
	    }
	    return obj;
	}
	
	public <B, A, c> B dd(c c){
	    return null;
	}
	
}
